# -*- coding: utf-8 -*-
'''
Created on Aug 13, 2012

@author: LONG HOANG GIANG
'''
import sys, os
import mechanize
from urlparse import urljoin
sys.path.append(os.path.expanduser('/home5/vietcntt/longhoanggiang/python'))
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
from CrawlerLib import commonlib
import datetime
import re
import simplejson as json

html_cfg = '''<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="content-type" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="format-detection" content="telephone=no" />
<style type="text/css">

p { margin: 0px; padding: 0px; }

body {
    text-align: justify;
    text-justify: newspaper;
    font-size: 1.2em;
}
</style>
</head>
<body>
#CONTENT#
</body>
</html>
</html>'''

OUTPUT = '/'
BASEURL = 'http://dochanh.net/'

cj = mechanize.CookieJar()
br = mechanize.Browser()
br.set_cookiejar(cj)
br.set_handle_referer(True)

def getHtml(url):
    res = br.open(url)
    return res.read()

def ensureOutput():
    print '> start check output folder'
    global OUTPUT
    OUTPUT = OUTPUT.strip()
    if OUTPUT == '' or OUTPUT == '/':
        OUTPUT = '/dochanh-output'
    if (not OUTPUT.endswith('/')): OUTPUT += '/'
    if not os.path.exists(OUTPUT): os.makedirs(OUTPUT, 0777)
    print '> saved file to {0}'.format(OUTPUT)

def process(url):
    ensureOutput()
    info = getInfo(url)
    maxPage = info['max']
    url = info['start_url']
    chapters = info['chapters']
    c = 0
    hasErr = False
    while c < maxPage or url == '' or url == BASEURL:
        pInfo = getPageInfo(url)
        if len(chapters.keys()) > 0:
            if chapters[pInfo['chap_group']]['start'] == 0:
                chapters[pInfo['chap_group']]['start'] = int(pInfo['current_chap'])
            chapters[pInfo['chap_group']]['end'] = int(pInfo['current_chap'])
        content = getHtmlContent(pInfo['c_id'], url)
        if content.strip() == '':
            print '---> Empty pages {0}'.format(url)
            hasErr = True
            break
        content = re.sub(r"(#CONTENT#)", content, html_cfg)
        commonlib.file_put_contents("{0}{1}.html".format(OUTPUT, pInfo['current_chap']), content)
        if pInfo['str_next'] == '' or pInfo['str_next'] == BASEURL: break
        url = pInfo['str_next']
    if not hasErr:
        f = open('{0}{1}'.format(OUTPUT, 'data'), 'w')
        json.dump(chapters.values(), f);
        f.close()

def getPageInfo(url):
    print '> process page {0}'.format(url)
    html = getHtml(url)
    cinfo = commonlib.extractText("(var str_prev=[^;]+);", html, 1)
    curent_chap = commonlib.extractText("curent_chap=(\d+)", cinfo, 1)
    chap_group = int(commonlib.extractText("chap_group_id=(\d+)", cinfo, 1))
    c_id = commonlib.extractText("c_id=(\d+)", cinfo, 1)
    str_next = urljoin(BASEURL, commonlib.extractText("str_next='([^']+)'", cinfo, 1))
    return {'current_chap': curent_chap, 'chap_group': chap_group, 'c_id': c_id, 'str_next': str_next}
    
    
def getInfo(url):
    print '> start get info of the story'
    if not re.search(r"/truyen-\d+/", url, re.IGNORECASE):
        print ':: -- this isn\'t story url'
        sys.exit(1)
    html = getHtml(url)
    info = commonlib.extractText("<script type=\"text/javascript\">(var pid=.+)</script>", html, 1)
    try:
        chap_group = eval(commonlib.extractText("var arr_chap_group=new Array\(([^)]+)", info, 1))
    except:
        chap_group = []
    chapters = {}
    for item in chap_group:
        chapters[item['id']] = {'name': item['ten'], 'start': 0, 'end': 0} 
    limit_chap = int(commonlib.extractText("limit_chap=(\d+)", info, 1))
    start_reading = commonlib.extractText("start_reading='([^']+)'", info, 1)
    if start_reading.strip() == '':
        print ':: -- get start_reading error'
        sys.exit(1)
    start_reading = urljoin(BASEURL, start_reading)
    return {'max': limit_chap, 'start_url': start_reading, 'chapters': chapters}

def getHtmlContent(pageId, url):
    url = 'http://dochanh.net/singleee/{0}'.format(pageId)
    html = getHtml(url)
    return html
    

if __name__ == '__main__':
    
    print getPageInfo('http://dochanh.net/truyen-12492/phe-hon-nghich-thien.html')
    
#    process('http://dochanh.net/truyen-12368/cho-anh-hon-em-mot-cai-nao.html')
    
    print '> Finished at {0}'.format(datetime.datetime.now())
    os._exit(1)

